﻿using HidSharp;
using OpenHardwareMonitor.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace customized_keyboard
{
    public partial class mainform : Form
    {
        hiddef Hids = new hiddef();
        HidSharp.HidDevice[] hids;
        HidSharp.DeviceList hidlist = HidSharp.DeviceList.Local;
        int getItem = 0;
        int modechangeflag = 0;
        int inireadflag = 0;
        public int workflag = 0;//正常工作标志
        HidSharp.HidStream hidStream;//数据流
        const int BUFFSIZE = 256;
        byte[] buffers = new byte[BUFFSIZE];//接收数据缓存
        byte[] buffers2 = new byte[BUFFSIZE];
        object stateobj = null;
        int recordnum = 0;
        byte[] readBuffers = new byte[BUFFSIZE];//接收数组
        int readnum = 0;//接收数量
        //int readFlag = 0;//接收数据是否处理完标志
        private delegate void delegateSetTextbox1(byte[] values);
        private delegate void delegateSetbutton1();
        string linkstr;
        key_Info[] key_info = new key_Info[30];//键盘数组，还得初始化内部数组！
        MyDictionary myDictionary = new MyDictionary();
        key_Info[] node_info = new key_Info[2];

        //Thread thread1 = new Thread(new ThreadStart(Thread1));//新开线程处理数据

        int key_layer_1_readflag = 0;
        int key_layer_2_readflag = 0;
        int key_layer_3_readflag = 0;
        int node_readflag = 0;
        int selectkey = 100;//当前的按键号，0-29为按键的，30-31为旋钮的

        byte[] writeBuffers = new byte[65];//发送数组
        INIOperationClass inideal = new INIOperationClass();
        string file = System.Windows.Forms.Application.StartupPath + "\\HidIni.ini";

        int firstbeginflag = 0;//设置启动时的标记，避免触发控件



        string[] boxvalues = { "1", "2", "3" };

        private void Setbutton1(byte[] values)
        {
            button6.Enabled = false;
            button3.Enabled = true;
            button8.Enabled = false;
            button3.Text = "连接";
            label3.Text = "";
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            keyinfoBOX.Text = "";
        }


        private void SetTextbox1(byte[] values)
        {
            if (recordnum < 100) recordnum += 1;
            else
            {
                textBox1.Text = "";
                recordnum = 0;
            }
            foreach (var buffer in values)
            {

                textBox1.Text += " " + "0x" + Convert.ToString(buffer, 16); 
            }
            textBox1.Text += Environment.NewLine;
            //readFlag = 0;
        }

        struct key_Info//按键状态结构体
        {
            public int keySN;
            public int keymode;
            public int keynum;
            public int[] keyvalue;
        };

        public mainform()
        {
            InitializeComponent();
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
            this.listView1.Columns.Add("序号", 40);   //设置listView列标题头
            this.listView1.Columns.Add("设备", 1000);
            label3.Text = "";
            button6.Enabled = false;
            button8.Enabled = false;
            if (File.Exists(file))
            {
                if (inideal.INIGetStringValue(file, "HID", "hid", null) == null)
                {
                    inideal.INIWriteValue(file, "HID", "hid", "?");

                }
                else
                {
                    linkstr = inideal.INIGetStringValue(file, "HID", "hid", null);
                    inireadflag = 1;
                }
            }
            else
            {
                inideal.INIWriteValue(file, "HID", "hid", "?");
            }
            foreach (var boxvalue in boxvalues) { comboBox1.Items.Add(boxvalue); }
            firstbeginflag = 1;
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("1");
            comboBox1.SelectedIndex = 0;
            firstbeginflag = 0;
            for (int i = 0; i < 30; i++)
            {
                key_info[i].keyvalue = new int[100];
            }
            for (int i = 0; i < 2; i++)
            {
                node_info[i].keyvalue = new int[10];
            }
            label8.Text = "";

            //var firstKey = myDictionary.NORMAL_KEY_DICT.FirstOrDefault(q => q.Value == 0X04).Key;


        }

        private void button1_Click(object sender, EventArgs e)//刷新按钮
        {
            hidlist = Hids.GetHidDevice();
            hids = hidlist.GetHidDevices().ToArray();
            listView1.Items.Clear();
            var i = 0;
            this.listView1.BeginUpdate();
            foreach (HidSharp.Device dev in hids)
            {
                ListViewItem item = new ListViewItem();
                item.Text = i.ToString();
                item.SubItems.Add(dev.ToString());
                listView1.Items.Add(item);
                i += 1;
            }
            this.listView1.EndUpdate();
            modechangeflag = 2;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (modechangeflag == 0)
            {
                MessageBox.Show("请先刷新!");
                return;
            }
            listView1.Items.Clear();
            var i = 0;
            if (modechangeflag == 1)
            {
                this.listView1.BeginUpdate();
                foreach (HidSharp.Device dev in hids)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = i.ToString();
                    item.SubItems.Add(dev.ToString());
                    listView1.Items.Add(item);
                    i += 1;
                }
                this.listView1.EndUpdate();
                modechangeflag = 2;
            }
            else if (modechangeflag == 2)
            {
                this.listView1.BeginUpdate();
                foreach (HidSharp.Device dev in hids)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = i.ToString();
                    item.SubItems.Add(dev.DevicePath);
                    listView1.Items.Add(item);
                    i += 1;
                }
                this.listView1.EndUpdate();
                modechangeflag = 1;
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            modechangeflag = 0;
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            else
            {
                getItem = int.Parse(listView1.SelectedItems[0].Text);
                vidtext.Text = hids[getItem].VendorID.ToString();
                pidtext.Text = hids[getItem].ProductID.ToString();
                inireadflag = 0;
                //Console.WriteLine(hids[getItem].DevicePath);
            }

        }



        private void button3_Click(object sender, EventArgs e)//打开按钮
        {
            var js = 0;
            if (inireadflag == 1)//试着从ini读取
            {
                hidlist = Hids.GetHidDevice();
                hids = hidlist.GetHidDevices().ToArray();
                getItem = 0;
                foreach (HidSharp.Device dev in hids)
                {
                    js++;
                }
                foreach (HidSharp.Device dev in hids)
                {
                    if (dev.DevicePath == linkstr)
                    {
                        break;
                    }
                    getItem++;
                }
                if (js == getItem)
                {
                    inireadflag = 0;
                    MessageBox.Show("请先刷新!");
                    return;
                }

            }
            else if (modechangeflag == 0)
            {
                MessageBox.Show("请先刷新!");
                return;
            }

            if (hids[getItem].TryOpen(out hidStream))
            {
                byte[] buffers7 = new byte[65];
                //MessageBox.Show("打开成功!");
                try { hidStream.Write(buffers7); }
                catch (Exception err)
                {
                    Console.WriteLine(err + "send erro");
                    MessageBox.Show("打开失败!");
                    return;
                }
                button3.Text = "已连接";
                button3.Enabled = false;
                button6.Enabled = true;
                button8.Enabled = true;
                label3.Text = "已连接 VID：" + hids[getItem].VendorID.ToString() + " PID：" + hids[getItem].ProductID.ToString() + "设备";
                workflag = 1;

                inideal.INIWriteValue(file, "HID", "hid", hids[getItem].DevicePath);
                hidStream.BeginRead(buffers, 0, BUFFSIZE, new AsyncCallback(ReadCallBack), stateobj);
                //thread1.Start();//新线程开始
            }
            else
            {
                MessageBox.Show("打开失败!");
                //button3.Text = "打开";
            }
        }

        private void button6_Click(object sender, EventArgs e)//关闭按钮
        {
            workflag = 0;
            try { hidStream.Close(); }
            catch (Exception err)
            {
                Console.WriteLine(err + "erro");
            }
            button3.Text = "打 开";
            button6.Enabled = false;
            button3.Enabled = true;
            button8.Enabled = false;
            label3.Text = "";
            modechangeflag = 0;
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            keyinfoBOX.Text = "";
        }

        int startflag = 0;
        private void ReadCallBack(IAsyncResult ar)
        {
            HidStream handler = hidStream;
            int bytesread = 0;
            int iflag = 0;

            if (workflag == 0)
            {
                try { bytesread = handler.EndRead(ar); }
                catch (Exception err)
                {
                    Console.WriteLine(err + "erro");
                }
                Array.Clear(buffers, 0, BUFFSIZE);
                BeginInvoke(new delegateSetTextbox1(Setbutton1), readBuffers);
                return;
            }
            try
            {
                if (handler.CanRead)
                {
                    bytesread = handler.EndRead(ar);
                    //Console.WriteLine("bytesread=" + bytesread.ToString("D"));
                    if (bytesread > 0)
                    {
                        Array.Copy(buffers, 1, buffers2, 0, buffers.Length - 1);
                        //Console.WriteLine(" buffers[0]=" + buffers2[0]);
                        if (0xEA == buffers2[0] && startflag == 0)
                        {
                            readnum = 0;
                            //Console.WriteLine(startflag);
                            startflag = 1;
                        }

                        if (startflag == 1)
                        {
                            for (int i = 0; i < bytesread - 1; i++)
                            {
                                readBuffers[readnum + i] = buffers2[i]; //把接收到的数据送到自定义的缓存区保存（Report_buf[i]为USB的接收缓存区）
                                iflag++;
                                if (readBuffers[readnum + i] == 0xE9 && readBuffers[readnum + i - 1] == 0xE8)
                                {
                                    startflag = 0;
                                    //USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, recordbuffer, 64);
                                    break;
                                }
                            }
                            readnum += iflag;
                        }

                        if (readnum > 256)
                        {
                            readnum = 0;
                            startflag = 0;
                        }
                        textBox1.BeginInvoke(new delegateSetTextbox1(SetTextbox1), readBuffers);//非ui线程更新
                        dealreadbuffer();//处理数据


                    }
                }
            }
            catch (Exception err)
            {
                //Console.WriteLine("1 " + err.Message);

                if (err.Message.Contains("Operation failed early"))
                {
                    MessageBox.Show("设备已拔出!");
                    //bytesread = handler.EndRead(ar);
                    BeginInvoke(new delegateSetTextbox1(Setbutton1), readBuffers);
                    //need to call StopWork
                    workflag = 0;
                    return;
                }
                else if (err.Message.Contains("Closed"))
                {
                    //do not execute BeginRead
                    MessageBox.Show("设备异常关闭!");
                    //bytesread = handler.EndRead(ar);
                    BeginInvoke(new delegateSetTextbox1(Setbutton1), readBuffers);
                    workflag = 0;
                    return;
                }
            }
            try
            {
                Array.Clear(buffers, 0, BUFFSIZE);
                handler.BeginRead(buffers, 0, BUFFSIZE, new AsyncCallback(ReadCallBack), stateobj);
            }
            catch (Exception err)
            {
                Console.WriteLine("2 " + err.Message);
                BeginInvoke(new delegateSetTextbox1(Setbutton1), readBuffers);
                workflag = 0;
            }
        }

        private void dealreadbuffer()
        {
            if (startflag == 1) return;//还在读数据
            if (readBuffers[1] == 'A' && readBuffers[readnum - 1] == 0xE9
            && readBuffers[readnum - 2] == 0xE8)
            {
                switch (readBuffers[2])
                { //分别进入对应函数
                    case (int)'C':
                        Info_Deal_Command();
                        break;
                    case (int)'L':
                        Info_Deal_Light();
                        break;
                    case (int)'D':
                        Info_Deal_Display();
                        break;
                    case (int)'K':
                        Info_Deal_Key();
                        break;
                    default:
                        break;
                }
                readnum = 0;
                startflag = 0;
                //memset(UsbBuffer, 0, buffernum);
                return;
            }
            else
            { //数据错误就清空数组
                readnum = 0;
                startflag = 0;
                return;
            }
        }

        private void Info_Deal_Key()
        {

            int layer = 0;
            if (readBuffers[3] == (int)'K' && readBuffers[4] == (int)'R' && readBuffers[5] == (int)'R')//按键返回的数据 
            {
                layer = readBuffers[6];
                key_info[layer * 10 + readBuffers[7] - 1].keySN = readBuffers[7];
                key_info[layer * 10 + readBuffers[7] - 1].keymode = readBuffers[8];
                key_info[layer * 10 + readBuffers[7] - 1].keynum = readBuffers[9];
                //Console.WriteLine(readBuffers[10]);
                Array.Copy(readBuffers, 10, key_info[layer * 10 + readBuffers[7] - 1].keyvalue, 0, key_info[layer * 10 + readBuffers[7] - 1].keynum);
                //Array.Copy(readBuffers, key_info[layer * 10 + readBuffers[7] - 1].keyvalue, key_info[layer * 10 + readBuffers[7] - 1].keynum);
                //Console.WriteLine(key_info[layer * 10 + readBuffers[7] - 1].keyvalue[0]);
            }
            else if (readBuffers[3] == (int)'N' && readBuffers[4] == (int)'R' && readBuffers[5] == (int)'R')
            {
                node_info[readBuffers[6]-1].keySN = readBuffers[6];
                //Console.WriteLine(readBuffers[6]);
                node_info[readBuffers[6]-1].keymode = readBuffers[7];
                node_info[readBuffers[6]-1].keynum = readBuffers[8];
                //Console.WriteLine(readBuffers[10]);
                Array.Copy(readBuffers, 8, node_info[readBuffers[6]-1].keyvalue, 0, node_info[readBuffers[6]-1].keynum);
            }
            else if (readBuffers[3] == (int)'K' && readBuffers[4] == (int)'L' && readBuffers[5] == (int)'R') //按键层数返回
            {
                MessageBox.Show("已切换至" + (readBuffers[6] + 1).ToString() + "层");
            }
        }

        private void Info_Deal_Display()
        {
            throw new NotImplementedException();
        }

        private void Info_Deal_Light()
        {
            throw new NotImplementedException();
        }

        private void Info_Deal_Command()
        {
            throw new NotImplementedException();
        }

        private void node_button(object sender, EventArgs e)
        {
            if (node_readflag == 0)
            {
                MessageBox.Show("请先读取配置!");
                return;
            }
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            string[] keybuttons_str = { "key_1", "key_2", "key_3", "key_4", "key_5", "key_6", "key_7", "key_8", "key_9", "key_10", "key_zx", "key_yx" };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            (sender as Button).BackColor = Color.Pink;
            keyinfoBOX.Text = "";
            var sn = Array.IndexOf(keybuttons_str, (sender as Button).Name) - 10;
            selectkey = sn + 30;
            keyinfoBOX.Text += "按键序号：" + node_info[sn].keySN;
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键模式：" + keymodes(node_info[sn].keymode);
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键数量：" + node_info[sn].keynum;
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键值：";
            if (node_info[sn].keymode == 3)
            {
                foreach (var value in node_info[sn].keyvalue) {
                    if (value != 0) keyinfoBOX.Text += myDictionary.MEDIA_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }
            else
            {

                foreach (var value in node_info[sn].keyvalue)
                {
                    if (value != 0) keyinfoBOX.Text += myDictionary.NORMAL_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }

        }
        private void mousemove_nodebutton(object sender, EventArgs e)//按键提示
        {
            //Console.WriteLine("123");
            if (node_readflag == 0)
            {
                return;
            }
            var toolTip1 = new ToolTip();
            string[] keybuttons_str = { "key_zx", "key_yx" };
            var sn = Array.IndexOf(keybuttons_str, (sender as Button).Name);
            string text = "";
            text += "按键序号：" + node_info[sn].keySN;
            text += Environment.NewLine;
            text += "按键模式：" + keymodes(node_info[sn].keymode);
            text += Environment.NewLine;
            text += "按键数量：" + node_info[sn].keynum;
            text += Environment.NewLine;
            text += "按键值：";
            if (node_info[sn].keymode == 3)
            {
                foreach (var value in node_info[sn].keyvalue) {
                    if(value!=0) text += myDictionary.MEDIA_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";

                }
            }
            else
            {

                foreach (var value in node_info[sn].keyvalue)
                {
                    if (value != 0) text += myDictionary.NORMAL_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }
            toolTip1.SetToolTip(sender as Button, text);
        }




        private void key_button(object sender, EventArgs e)
        {
            if ((key_layer_1_readflag != 1 && comboBox1.SelectedIndex == 0) || (key_layer_2_readflag != 1 && comboBox1.SelectedIndex == 1) || (key_layer_3_readflag != 1 && comboBox1.SelectedIndex == 2))
            {
                MessageBox.Show("请先读取配置!");
                return;
            }
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            string[] keybuttons_str = { "key_1", "key_2", "key_3", "key_4", "key_5", "key_6", "key_7", "key_8", "key_9", "key_10", "key_zx", "key_yx" };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            //var btnName=(sender as Button).Name;
            (sender as Button).BackColor = Color.Pink;
            //Console.WriteLine(Array.IndexOf(keybuttons_str, (sender as Button).Name));
            keyinfoBOX.Text = "";
            selectkey = Array.IndexOf(keybuttons_str, (sender as Button).Name);
            var sn = comboBox1.SelectedIndex * 10 + Array.IndexOf(keybuttons_str, (sender as Button).Name);
            keyinfoBOX.Text += "按键序号：" + key_info[sn].keySN;
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键模式：" + keymodes(key_info[sn].keymode);
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键数量：" + key_info[sn].keynum;
            keyinfoBOX.Text += Environment.NewLine;
            keyinfoBOX.Text += "按键值：";
            if (key_info[sn].keymode == 3)
            {
                foreach (var value in key_info[sn].keyvalue) {
                    if (value != 0) keyinfoBOX.Text += myDictionary.MEDIA_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }
            else
            {

                foreach (var value in key_info[sn].keyvalue)
                {
                    if (value != 0) keyinfoBOX.Text += myDictionary.NORMAL_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }

        }


        private void mousemove_keybutton(object sender, EventArgs e)//按键提示
        {
            //Console.WriteLine("123");
            if ((key_layer_1_readflag != 1 && comboBox1.SelectedIndex == 0) || (key_layer_2_readflag != 1 && comboBox1.SelectedIndex == 1) || (key_layer_3_readflag != 1 && comboBox1.SelectedIndex == 2))
            {
                return;
            }
            var toolTip1 = new ToolTip();
            //Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10 };
            string[] keybuttons_str = { "key_1", "key_2", "key_3", "key_4", "key_5", "key_6", "key_7", "key_8", "key_9", "key_10" };
            var sn = comboBox1.SelectedIndex * 10 + Array.IndexOf(keybuttons_str, (sender as Button).Name);
            string text = "";
            //selectkey = sn;
            text += "按键序号：" + key_info[sn].keySN;
            text += Environment.NewLine;
            text += "按键模式：" + keymodes(key_info[sn].keymode);
            text += Environment.NewLine;
            text += "按键数量：" + key_info[sn].keynum;
            text += Environment.NewLine;
            text += "按键值：";
            if (key_info[sn].keymode == 3)
            {
                foreach (var value in key_info[sn].keyvalue) {
                    if (value != 0)  text += myDictionary.MEDIA_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }
            else
            {
                foreach (var value in key_info[sn].keyvalue)
                {
                    if (value != 0) text += myDictionary.NORMAL_KEY_DICT.FirstOrDefault(q => q.Value == value).Key + " ";
                }
            }
            toolTip1.SetToolTip(sender as Button, text);
        }


        private string keymodes(int num)
        {
            switch (num)
            {
                case 0:
                    return "单按键模式";
                case 2:
                    return "长按键模式";
                case 3:
                    return "多媒体按键模式";
                case 4:
                    return "特殊按键模式";
                default:
                    return "读取错误";
            }
        }

        private void button7_Click(object sender, EventArgs e)//按键配置窗口
        {
            if ((key_layer_1_readflag != 1 && comboBox1.SelectedIndex == 0) || (key_layer_2_readflag != 1 && comboBox1.SelectedIndex == 1) || (key_layer_3_readflag != 1 && comboBox1.SelectedIndex == 2))
            {
                MessageBox.Show("请先读取配置!");
                return;
            }
            if (node_readflag != 1 && selectkey >= 30)
            {
                MessageBox.Show("请先读取配置!");
                return;
            }
            if (selectkey == 100)
            {
                MessageBox.Show("请先选择按键!");
                return;
            }

            getKeyCode f2 = new getKeyCode();
            f2.ShowDialog();
            string str = "";
            if (f2.DialogResult == DialogResult.OK)
            {
                str = f2.keycodestr;
                f2.Close();
                //Console.WriteLine(xx);
            }
            var keymode = 0;
            if (string.IsNullOrEmpty(str)) return;
            if (str[0] == 'N')
            {
                keymode = 0;
                str = str.Substring(1, str.Length - 1);
                string[] resultString = str.Split(new Char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
                var num = resultString.Length;
                if (num > 1) keymode = 2;
                else if (num == 1)//只有一个按键，判断是不是特殊按键
                {
                    var str2 = myDictionary.NORMAL_KEY_DICT[resultString[0]];
                    if (str2 == 0xE0 || str2 == 0xE1 || str2 == 0xE2 || str2 == 0xE3 || str2 == 0xE4 || str2 == 0xE5 || str2 == 0xE6 || str2 == 0xE7)
                    {
                        keymode = 4;
                    }
                }
                if (selectkey == 30 || selectkey == 31) //旋钮
                {
                    byte[] buffers = new byte[65];
                    Array.Clear(buffers, 0, buffers.Length);
                    if (num > 10)
                    {
                        MessageBox.Show("旋钮无法接受过多数据!");
                        return;
                    }
                    buffers[1] = 0xEA;
                    buffers[2] = (int)('A');
                    buffers[3] = (int)('K');
                    buffers[4] = (int)('N');
                    buffers[5] = (int)('W');
                    buffers[6] = (byte)(selectkey - 30 + 1);
                    buffers[7] = (byte)keymode;
                    buffers[8] = (byte)num;
                    num = 0;
                    foreach (var str1 in resultString)
                    {
                        buffers[num + 9] = myDictionary.NORMAL_KEY_DICT[str1];
                        //Console.WriteLine(str1);
                        num++;
                    }
                    buffers[num + 9] = 0xE8;
                    buffers[num + 9 + 1] = 0xE9;
                    try { hidStream.Write(buffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(100);


                    //然后读一下
                    Array.Clear(writeBuffers, 0, writeBuffers.Length);
                    writeBuffers[1] = 0xEA;
                    writeBuffers[2] = (int)('A');
                    writeBuffers[3] = (int)('K');
                    writeBuffers[4] = (int)('N');
                    writeBuffers[5] = (int)('R');
                    writeBuffers[6] = (byte)(selectkey - 30 + 1);
                    writeBuffers[7] = 0xE8;
                    writeBuffers[8] = 0xE9;
                    try { hidStream.Write(writeBuffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);//延时


                }
                else if (selectkey < 30) //按键
                {
                    byte[] bufferss = new byte[100];
                    byte[] buffers = new byte[65];
                    Array.Clear(bufferss, 0, bufferss.Length);
                    Array.Clear(buffers, 0, buffers.Length);
                    bufferss[1] = 0xEA;
                    bufferss[2] = (int)('A');
                    bufferss[3] = (int)('K');
                    bufferss[4] = (int)('W');
                    bufferss[5] = (byte)(selectkey + 1);
                    bufferss[6] = (byte)keymode;
                    bufferss[7] = (byte)num;
                    num = 0;
                    foreach (var str1 in resultString)
                    {
                        bufferss[num + 8] = myDictionary.NORMAL_KEY_DICT[str1];
                        //Console.WriteLine(str1);
                        num++;
                    }
                    bufferss[num + 8] = 0xE8;
                    bufferss[num + 8 + 1] = 0xE9; 
                    num = num + 9;
                    var sendflag = 0;
                    while (num > 64) //传出64一篇
                    {
                        Array.Copy(bufferss, 1 + sendflag * 64, buffers, 1, 64);
                        num -= 64;
                        sendflag++;
                        try { hidStream.Write(buffers); }
                        catch (Exception err)
                        {
                            Console.WriteLine(err + "send erro");
                        }
                        Thread.Sleep(100);
                    }
                    //传出不足64的页
                    Array.Copy(bufferss, 1 + sendflag * 64, buffers, 1, num);
                    try { hidStream.Write(buffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);

                    //然后读一下
                    Array.Clear(writeBuffers, 0, writeBuffers.Length);
                    writeBuffers[1] = 0xEA;
                    writeBuffers[2] = (int)('A');
                    writeBuffers[3] = (int)('K');
                    writeBuffers[4] = (int)('K');
                    writeBuffers[5] = (int)('R');
                    writeBuffers[6] = (byte)comboBox1.SelectedIndex;
                    writeBuffers[7] = (byte)(selectkey + 1);
                    writeBuffers[8] = 0xE8;
                    writeBuffers[9] = 0xE9;
                    try { hidStream.Write(writeBuffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);//延时
                }

            }
            else if (str[0] == 'M')
            {
                keymode = 3;
                str = str.Substring(1, str.Length - 1);
                if (selectkey == 30 || selectkey == 31) //旋钮
                {
                    byte[] buffers = new byte[65];
                    Array.Clear(buffers, 0, buffers.Length);
                    buffers[1] = 0xEA;
                    buffers[2] = (int)('A');
                    buffers[3] = (int)('K');
                    buffers[4] = (int)('N');
                    buffers[5] = (int)('W');
                    buffers[6] = (byte)(selectkey - 30 + 1);
                    buffers[7] = (byte)keymode;
                    buffers[8] = (byte)1;
                    buffers[9]=myDictionary.MEDIA_KEY_DICT[str];
                    buffers[10] = 0xE8; 
                    buffers[11] = 0xE9;
                    try { hidStream.Write(buffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(100);


                    //然后读一下
                    Array.Clear(writeBuffers, 0, writeBuffers.Length);
                    writeBuffers[1] = 0xEA;
                    writeBuffers[2] = (int)('A');
                    writeBuffers[3] = (int)('K');
                    writeBuffers[4] = (int)('N');
                    writeBuffers[5] = (int)('R');
                    writeBuffers[6] = (byte)(selectkey - 30 + 1);
                    writeBuffers[7] = 0xE8;
                    writeBuffers[8] = 0xE9;
                    try { hidStream.Write(writeBuffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);//延时


                }
                else if (selectkey < 30) //按键
                {
                    byte[] buffers = new byte[65];
                    Array.Clear(buffers, 0, buffers.Length);
                    buffers[1] = 0xEA;
                    buffers[2] = (int)('A');
                    buffers[3] = (int)('K');
                    buffers[4] = (int)('W');
                    buffers[5] = (byte)(selectkey + 1);
                    buffers[6] = (byte)keymode;
                    buffers[7] = (byte)1;
                    buffers[8]=myDictionary.MEDIA_KEY_DICT[str];
                    buffers[9] = 0xE8;
                    buffers[10] = 0xE9;
                    try { hidStream.Write(buffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);

                    //然后读一下
                    Array.Clear(writeBuffers, 0, writeBuffers.Length);
                    writeBuffers[1] = 0xEA;
                    writeBuffers[2] = (int)('A');
                    writeBuffers[3] = (int)('K');
                    writeBuffers[4] = (int)('K');
                    writeBuffers[5] = (int)('R');
                    writeBuffers[6] = (byte)comboBox1.SelectedIndex;
                    writeBuffers[7] = (byte)(selectkey + 1);
                    writeBuffers[8] = 0xE8;
                    writeBuffers[9] = 0xE9;
                    try { hidStream.Write(writeBuffers); }
                    catch (Exception err)
                    {
                        Console.WriteLine(err + "send erro");
                    }
                    Thread.Sleep(200);//延时
                }
            }
            //清空
            keyinfoBOX.Text = " ";
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            selectkey = 100;




        }
        //ASCIIEncoding charToASCII = new ASCIIEncoding();
        byte csflag = 0;
        private void button8_Click(object sender, EventArgs e)//测试按钮
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (int)('A');
            writeBuffers[3] = (int)('C');
            writeBuffers[4] = csflag;
            writeBuffers[5] = 0xE8;
            writeBuffers[6] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
            if (csflag == 0) csflag = 1;
            else csflag = 0;


        }


        private void button5_Click(object sender, EventArgs e)//读取配置
        {
            //int raedlayer=comboBox1.SelectedIndex;
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            label8.Text = "读取中";
            for (int i = 0; i < 10; i++)//读取按键
            {
                Array.Clear(writeBuffers, 0, writeBuffers.Length);
                writeBuffers[1] = 0xEA;
                writeBuffers[2] = (byte)('A');
                writeBuffers[3] = (byte)('K');
                writeBuffers[4] = (byte)('K');
                writeBuffers[5] = (byte)('R');
                writeBuffers[6] = (byte)comboBox1.SelectedIndex;
                writeBuffers[7] = (byte)(i + 1);
                writeBuffers[8] = 0xE8;
                writeBuffers[9] = 0xE9;
                try { hidStream.Write(writeBuffers); }
                catch (Exception err)
                {
                    Console.WriteLine(err + "send erro");
                }
                Thread.Sleep(500);//延时
            }



            label8.Text = "";
            if (key_info[9].keyvalue[0] != 0)
            {
                key_layer_1_readflag = 1;
                label4.Text = "已读取";
            }
            if (key_info[19].keyvalue[0] != 0)
            {
                key_layer_2_readflag = 1;
                label4.Text = "已读取";
            }
            if (key_info[29].keyvalue[0] != 0)
            {
                key_layer_3_readflag = 1;
                label4.Text = "已读取";
            }


        }
        private void button17_Click(object sender, EventArgs e)//旋钮读取
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            for (int i = 0; i < 2; i++)//读取旋钮
            {
                Array.Clear(writeBuffers, 0, writeBuffers.Length);
                writeBuffers[1] = 0xEA;
                writeBuffers[2] = (int)('A');
                writeBuffers[3] = (int)('K');
                writeBuffers[4] = (int)('N');
                writeBuffers[5] = (int)('R');
                writeBuffers[6] = (byte)(i + 1);
                writeBuffers[7] = 0xE8;
                writeBuffers[8] = 0xE9;
                try { hidStream.Write(writeBuffers); }
                catch (Exception err)
                {
                    Console.WriteLine(err + "send erro");
                }
                Thread.Sleep(300);//延时
            }


            if (node_info[1].keyvalue[0] != 0)
            {
                node_readflag = 1;
            }
        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (firstbeginflag == 1) return;
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            if (key_layer_1_readflag == 1 && comboBox1.SelectedIndex == 0)
            {
                label4.Text = "已读取";
            }
            else if (key_layer_2_readflag == 1 && comboBox1.SelectedIndex == 1)
            {
                label4.Text = "已读取";
            }
            else if (key_layer_3_readflag == 1 && comboBox1.SelectedIndex == 2)
            {
                label4.Text = "已读取";
            }
            else
            {
                label4.Text = "未读取";
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (int)('A');
            writeBuffers[3] = (int)('K');
            writeBuffers[4] = (int)('L');
            writeBuffers[5] = (int)('W');
            writeBuffers[6] = (byte)comboBox1.SelectedIndex;
            writeBuffers[7] = 0xE8;
            writeBuffers[8] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
            Thread.Sleep(100);


            //改了之后读取下
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (int)('A');
            writeBuffers[3] = (int)('K');
            writeBuffers[4] = (int)('L');
            writeBuffers[5] = (int)('R');
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
            Thread.Sleep(100);

            keyinfoBOX.Text = " ";
            Button[] keybuttons = { key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_10, key_zx, key_yx };
            foreach (var keybutton in keybuttons)
            {
                keybutton.BackColor = Color.Transparent;
            }
            selectkey = 100;

        }



        public class UpdateVisitor : IVisitor
        {
            public void VisitComputer(IComputer computer)
            {
                computer.Traverse(this);
            }
            public void VisitHardware(IHardware hardware)
            {
                hardware.Update();
                foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
            }
            public void VisitSensor(ISensor sensor) { }
            public void VisitParameter(IParameter parameter) { }
        }

        private static void GetSystemInfo()
        {
            UpdateVisitor updateVisitor = new UpdateVisitor();
            Computer computer = new Computer();

            computer.Open();
            computer.CPUEnabled = true;
            computer.GPUEnabled = true;
            computer.HDDEnabled = true;
            computer.RAMEnabled = true;//开启cpu，内存，硬盘，内存
            computer.Accept(updateVisitor);
            for (int i = 0; i < computer.Hardware.Length; i++)
            {
                if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                    }
                }
            }
            computer.Close();
        }


        int timeflag = 0;
        int totalSelect = 0;
        int nowsend = 0;
        int hhflag = 0;//换行标志
        int nowhnum = 0;//当前行数
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeflag == 0)
            {
                return;
            }
            totalSelect = 0;
            computer2.Accept(updateVisitor);


            string[] str = new string[5];

            try
            {
                combox4update();
                str[0] = "CPU " + textBox2.Text + textBox3.Text;
            }
            catch (Exception err) { str[0] = ""; }
            try
            {
                combox7update();
                str[1] = "GPU " + textBox5.Text + textBox4.Text;
            }
            catch (Exception err) { str[1] = ""; }
            try
            {
                combox8update();
                str[2] = "RAM " + textBox7.Text + textBox6.Text;
            }
            catch (Exception err) { str[2] = ""; }
            try
            {
                combox10update();
                str[3] = "HDD " + textBox9.Text + textBox8.Text;
            }
            catch (Exception err) { str[3] = ""; }

            CheckBox[] chexs = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
            CheckBox[] chexs1 = new CheckBox[5];
            foreach (var chex in chexs)
            {
                if (chex.Checked)
                {
                    chexs1[totalSelect] = chex;
                    totalSelect++;
                }
            }

            str[4] = textBox11.Text;

            var strsend = str[Array.IndexOf(chexs, chexs1[nowsend])];
            if (comboBox2.SelectedIndex == 0) { hhflag = 4; }
            else { hhflag = 2; }

            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (int)('A');
            writeBuffers[3] = (int)('D');
            writeBuffers[4] = (int)('W');
            writeBuffers[5] = (byte)nowhnum;
            if (hhflag == 4) writeBuffers[6] = 01;
            else if (hhflag == 2) writeBuffers[6] = 02;
            if (hhflag == 4 && strsend.Length > 16)
            {
                for (int i = 0; i < 16; i++) writeBuffers[7 + i] = (byte)strsend[i];
                writeBuffers[23] = 0xE8;
                writeBuffers[24] = 0xE9;
            }
            else if (hhflag == 2 && strsend.Length > 8)
            {
                for (int i = 0; i < 8; i++) writeBuffers[7 + i] = (byte)strsend[i];
                writeBuffers[15] = 0xE8;
                writeBuffers[16] = 0xE9;
            }
            else
            {
                int x = 0;
                foreach (var strsen in strsend)
                {
                    writeBuffers[7 + x] = (byte)strsen;
                    x++;
                }
                writeBuffers[7 + x] = 0xE8;
                writeBuffers[7 + x + 1] = 0xE9;

            }

            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
            nowsend++;
            nowhnum++;
            if (nowsend == totalSelect) { nowsend = 0; }
            if (nowhnum == hhflag) { nowhnum = 0; }
        }
        //int timeflag = 0;

        UpdateVisitor updateVisitor = new UpdateVisitor();
        Computer computer2 = new Computer();

        private void button9_Click(object sender, EventArgs e)
        {
            //GetSystemInfo();
            computer2.Open();
            computer2.CPUEnabled = true;
            computer2.GPUEnabled = true;
            computer2.HDDEnabled = true;
            computer2.RAMEnabled = true;//开启cpu，内存，硬盘，内存
            computer2.Accept(updateVisitor);

            ComboBox[] comboxhj = { comboBox3, comboBox4, comboBox5, comboBox8, comboBox10 };
            foreach (var com in comboxhj) { com.Enabled = true; }


            comboBox8.Text = "";
            comboBox8.Items.Clear();
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.RAM)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        comboBox8.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                    }
                }
            }
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.HDD)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        comboBox10.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                    }
                }
            }
            //computer2.Close();
        }


        private void combox4update()
        {
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        if (comboBox3.SelectedIndex == 0)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            {
                                if (computer2.Hardware[i].Sensors[j].Name == comboBox4.SelectedItem.ToString())
                                {
                                    textBox2.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                }
                            }
                        }
                        if (comboBox3.SelectedIndex == 1)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                            {
                                if (computer2.Hardware[i].Sensors[j].Name == comboBox4.SelectedItem.ToString())
                                {
                                    textBox2.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                }
                            }
                        }
                        if (comboBox3.SelectedIndex == 2)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                            {
                                if (computer2.Hardware[i].Sensors[j].Name == comboBox4.SelectedItem.ToString())
                                {
                                    textBox2.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                }
                            }
                        }


                    }
                }
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

            combox4update();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox4.Text = "";
            comboBox4.Items.Clear();
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        if (comboBox3.SelectedIndex == 0)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            {
                                comboBox4.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                            }
                        }
                        if (comboBox3.SelectedIndex == 1)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                            {
                                comboBox4.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                            }
                        }
                        if (comboBox3.SelectedIndex == 1)
                        {
                            if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                            {
                                comboBox4.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                            }
                        }


                    }
                }
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox[] comboxhj = { comboBox6, comboBox7 };
            comboBox7.Items.Clear();
            foreach (var com in comboxhj)
            {
                com.Text = "";
                com.Enabled = true;
            }
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox7.Items.Clear();
            comboBox7.Text = "";
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (comboBox5.SelectedIndex == 0)
                {
                    if (computer2.Hardware[i].HardwareType == HardwareType.GpuNvidia)
                    {
                        for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                        {
                            if (comboBox6.SelectedIndex == 0)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }
                            if (comboBox6.SelectedIndex == 1)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }
                            if (comboBox6.SelectedIndex == 2)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }


                        }
                    }
                }
                if (comboBox5.SelectedIndex == 1)
                {
                    if (computer2.Hardware[i].HardwareType == HardwareType.GpuAti)
                    {
                        for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                        {
                            if (comboBox6.SelectedIndex == 0)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }
                            if (comboBox6.SelectedIndex == 1)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }
                            if (comboBox6.SelectedIndex == 2)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                                {
                                    comboBox7.Items.Add(computer2.Hardware[i].Sensors[j].Name);
                                }
                            }


                        }
                    }
                }
            }
        }


        private void combox7update()
        {
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (comboBox5.SelectedIndex == 0)
                {
                    if (computer2.Hardware[i].HardwareType == HardwareType.GpuNvidia)
                    {
                        for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                        {
                            if (comboBox6.SelectedIndex == 0)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }
                            if (comboBox6.SelectedIndex == 1)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }
                            if (comboBox6.SelectedIndex == 2)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }


                        }
                    }
                }
                if (comboBox5.SelectedIndex == 1)
                {
                    if (computer2.Hardware[i].HardwareType == HardwareType.GpuAti)
                    {
                        for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                        {
                            if (comboBox6.SelectedIndex == 0)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }
                            if (comboBox6.SelectedIndex == 1)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }
                            if (comboBox6.SelectedIndex == 2)
                            {
                                if (computer2.Hardware[i].Sensors[j].SensorType == SensorType.Fan)
                                {
                                    if (computer2.Hardware[i].Sensors[j].Name == comboBox7.SelectedItem.ToString())
                                    {
                                        textBox5.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                                    }
                                }
                            }


                        }
                    }
                }
            }
        }
        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            combox7update();
        }

        private void combox8update()
        {
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.RAM)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        if (comboBox8.SelectedItem != null && computer2.Hardware[i].Sensors[j].Name == comboBox8.SelectedItem.ToString())
                        {
                            if (comboBox8.SelectedItem.ToString() == "Memory") { textBox6.Text = "%"; }
                            else if (comboBox8.SelectedItem.ToString() == "Used Memory" || comboBox8.SelectedItem.ToString() == "Available Memory") { textBox6.Text = "GB"; }
                            textBox7.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                            //Console.WriteLine(computer2.Hardware[i].Sensors[j].Name); 
                        }

                    }
                }
            }

        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            combox8update();
        }

        private void combox10update()
        {
            for (int i = 0; i < computer2.Hardware.Length; i++)
            {
                if (computer2.Hardware[i].HardwareType == HardwareType.HDD)
                {
                    for (int j = 0; j < computer2.Hardware[i].Sensors.Length; j++)
                    {
                        if (comboBox10.SelectedItem != null && computer2.Hardware[i].Sensors[j].Name == comboBox10.SelectedItem.ToString())
                        {
                            //if (comboBox8.SelectedItem.ToString() == "Memory") { textBox6.Text = "%"; }
                            //else if (comboBox8.SelectedItem.ToString() == "Used Memory" || comboBox8.SelectedItem.ToString() == "Available Memory") { textBox6.Text = "GB"; }
                            textBox9.Text = computer2.Hardware[i].Sensors[j].Value.ToString();
                            //Console.WriteLine(computer2.Hardware[i].Sensors[j].Name); 
                        }

                    }
                }
            }
        }
        private void comboBox10_SelectedIndexChanged(object sender, EventArgs e)
        {
            combox10update();
        }
        static public bool VldInt(string num)
        {
            #region
            try
            {
                Convert.ToInt32(num);
                return true;
            }
            catch { return false; }
            #endregion
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                timer1.Stop();
                timer1.Enabled = false;
                return;
            }
            if (VldInt(textBox10.Text) == false)
            {
                MessageBox.Show("请输入正确数据!");
                textBox10.Text = "500";
                timer1.Stop();
                timer1.Enabled = false;
                return;
            }
            else if (Convert.ToInt32(textBox10.Text) < 500)
            {
                MessageBox.Show("请输入正确数据!");
                textBox10.Text = "500";
                timer1.Stop();
                timer1.Enabled = false;
                return;
            }
            if (timeflag == 0)
            {
                timeflag = 1;
                timer1.Interval = Convert.ToInt32(textBox10.Text);
                timer1.Enabled = true;
                timer1.Start();

                button10.Text = "关闭";


            }
            else
            {
                totalSelect = 0;
                nowsend = 0;
                timeflag = 0;
                timer1.Stop();
                timer1.Enabled = false;
                button10.Text = "打开";
            }
        }

        private void button11_Click(object sender, EventArgs e)//打开oled
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('D');
            writeBuffers[4] = (byte)('P');
            writeBuffers[5] = (byte)1;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button12_Click(object sender, EventArgs e)//关闭oled
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('D');
            writeBuffers[4] = (byte)('P');
            writeBuffers[5] = (byte)0;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button14_Click(object sender, EventArgs e)//测试oled
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('D');
            writeBuffers[4] = (byte)0;
            writeBuffers[5] = 0xE8;
            writeBuffers[6] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button13_Click(object sender, EventArgs e)//复位oled
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('D');
            writeBuffers[4] = (byte)('R');
            writeBuffers[5] = 0xE8;
            writeBuffers[6] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }
        Color color=Color.FromArgb(0, 0, 0);
        private void button15_Click(object sender, EventArgs e)
        {

            ColorDialog colorDia = new ColorDialog();

            if (colorDia.ShowDialog() == DialogResult.OK)
            {
                //获取所选择的颜色
                Color colorChoosed = colorDia.Color;
                //改变panel的背景色
                color = colorChoosed;
                int r = colorChoosed.R;
                int g = colorChoosed.G;
                int b = colorChoosed.B;
                pictureBox1.BackColor = Color.FromArgb(r, g, b);
                trackBar1.Value = r;
                trackBar2.Value = g;
                trackBar3.Value = b;
            }
        }



        private void trackbarchange(object sender, EventArgs e)
        {
            int r = trackBar1.Value;
            int g = trackBar2.Value;
            int b = trackBar3.Value;
            pictureBox1.BackColor = Color.FromArgb(r, g, b);
            color = Color.FromArgb(r, g, b);
        }
        private void button16_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('W');
            writeBuffers[5] = (byte)color.R;
            writeBuffers[6] = (byte)color.G;
            writeBuffers[7] = (byte)color.B;
            writeBuffers[8] = 0xE8;
            writeBuffers[9] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] =0;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] = 1;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] = 2;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] = 3;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] = 4;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }

        private void button23_Click(object sender, EventArgs e)
        {
            if (workflag == 0)
            {
                MessageBox.Show("请先连接!");
                return;
            }
            Array.Clear(writeBuffers, 0, writeBuffers.Length);
            writeBuffers[1] = 0xEA;
            writeBuffers[2] = (byte)('A');
            writeBuffers[3] = (byte)('L');
            writeBuffers[4] = (byte)('C');
            writeBuffers[5] = 5;
            writeBuffers[6] = 0xE8;
            writeBuffers[7] = 0xE9;
            try { hidStream.Write(writeBuffers); }
            catch (Exception err)
            {
                Console.WriteLine(err + "send erro");
            }
        }
    }
}